package com.csw.android.player

import com.csw.android.dev_utils.utils.LogUtils
import com.csw.android.player.base.IMediaPlayer
import com.csw.android.player.base.MediaPlayerType
import com.csw.android.player.exoplayer.ExoMediaPlayer

/**
 * 播放器管理器，播放器采用单例实现
 */
object PlayerManager {
    private val mediaPlayerType: MediaPlayerType = MediaPlayerType.EXOPLAYER
    private val playerMap = HashMap<String, IMediaPlayer>()

    /**
     * 单例播放器
     */
    val SINGLE_PLAYER = newPlayer()

    /**
     * 创建一个新的Player
     */
    fun newPlayer(): IMediaPlayer {
        LogUtils.i(this, "newPlayer curr ${playerMap.size}")
        return when (mediaPlayerType) {
            else -> ExoMediaPlayer()
        }.apply {
            playerMap[playId] = this
        }
    }

    /**
     * 获取Player对象
     */
    fun getPlayer(playerId: String): IMediaPlayer {
        return playerMap[playerId] ?: newPlayer()
    }

    /**
     * 移除Player对象
     */
    fun removePlayer(playerId: String) {
        playerMap.remove(playerId)
    }
}